package kr.co.sdsway2.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import kr.co.sdsway2.comm.DBUtil;
import kr.co.sdsway2.comm.SdsWay2Util;
import kr.co.sdsway2.vo.AvatarVO;

import org.springframework.stereotype.Repository;

@Repository
public class AvatarDAO {

	public AvatarVO selectAvatarInfByPk(String avatarId) {
		Connection conn =null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		AvatarVO avatarVO = null;
		String queryStr = "select AVATAR_ID, LEFT_POSITION, TOP_POSITION , AVATAR_TYPE from AVATAR where AVATAR_ID=?";
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(queryStr);
			//debug("query str : "+queryStr);
			//debug("avatarId : "+avatarId);
			ps.setString(1, avatarId);
			rs = ps.executeQuery();
			
			while(rs.next()){
				avatarVO = new AvatarVO();
				avatarVO.setAvatarId(rs.getString(1));
				avatarVO.setPosX(rs.getInt(2));
				avatarVO.setPosY(rs.getInt(3));
				avatarVO.setAvatarType(rs.getString(4));
			}
			return avatarVO;
		} catch (Exception e) {
			System.err.println("[ERROR] DB 오류 발생 ");
			e.printStackTrace();
		}
		
		return avatarVO;
	}

	public void updateAvatarPosition(String avatarId, String Xdist,
			String Ydist,String avatarType) {
		
//		debug("avatarId:"+avatarId+",Xdist:"+Xdist+",Ydist:"+Ydist+",avatarType:"+avatarType);
		
		Connection conn =null;
		PreparedStatement ps =null;
		ResultSet rs = null;
//		AvatarVO avatarVO = null;
		String queryStr = null;
		String updateStatement_queryStr = "";
		int resultCnt = -1;
		if(Integer.parseInt(Xdist)>0){
			updateStatement_queryStr = "left_position = left_position + "+Xdist+" ";
		}else if (Integer.parseInt(Xdist)<0){
			updateStatement_queryStr = "left_position = left_position "+Xdist+" ";
		}else{
			updateStatement_queryStr = "left_position = left_position ";
		}
		
		if(Integer.parseInt(Ydist)>0){
			updateStatement_queryStr += ",top_position = top_position + "+Ydist+" ";
		}else if (Integer.parseInt(Ydist)<0){
			updateStatement_queryStr += ",top_position = top_position "+Ydist+" ";
		}else{
			updateStatement_queryStr += ",top_position = top_position ";
		}
		
//		if("U".equals(direction)){
//			updateStatement_queryStr = "top_position = top_position - "+distance+" ";
//		}else if("D".equals(direction)){
//			updateStatement_queryStr = "top_position = top_position + "+distance+" ";
//		}else if("L".equals(direction)){
//			updateStatement_queryStr = "left_position = left_position - "+distance+" ";
//		}else if("R".equals(direction)){
//			updateStatement_queryStr = "left_position = left_position + "+distance+" ";
//		}
		if(!SdsWay2Util.isNull(avatarType)){
			updateStatement_queryStr += ",AVATAR_TYPE = " + avatarType+" ";
		}
		queryStr = "update AVATAR set " + updateStatement_queryStr +" where avatar_id = ?";
//		debug("query:"+queryStr);
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(queryStr);
			//debug("query str : "+queryStr);
			//debug("avatarId : "+avatarId);
			//debug("avatarType : "+avatarType);
			ps.setString(1, avatarId);
			resultCnt = ps.executeUpdate();
			
		} catch (Exception e) {
			System.err.println("[ERROR] DB 오류 발생 ");
			e.printStackTrace();
		} 
		
		if(resultCnt==1){
//			//debug("avatar Position update success!");
		}else{
			System.err.println("[ERROR] DB 업데이트 도중 에러 발생");
		}
	}

	public void insertAvatar(String avatarId,String avatarType) {
		Connection conn =null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		String queryStr = "INSERT INTO AVATAR (AVATAR_ID, LEFT_POSITION, TOP_POSITION ,AVATAR_TYPE ) VALUES (?, '100', '100' , ?);";
		int resultCnt = -1;

		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(queryStr);
			//debug("query str : "+queryStr);
			//debug("avatarId : "+avatarId);
			ps.setString(1, avatarId);
			ps.setString(2, avatarType);
			resultCnt = ps.executeUpdate();
			
		} catch (Exception e) {
			System.err.println("[ERROR] DB 오류 발생 ");
			e.printStackTrace();
		} 

		if(resultCnt==1){
//			//debug("avatar insert success!");
		}else{
			System.err.println("[ERROR] DB 삽입 도중 에러 발생");
		}
	}
	
	//모든 vo 가져옴.
	public List<AvatarVO> getAllAvatarId() {
		
		Connection conn =null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		AvatarVO avatarVO = null;
		List<AvatarVO> avatarVOList = null;
		String queryStr = "select AVATAR_ID, LEFT_POSITION, TOP_POSITION , AVATAR_TYPE from AVATAR";
		try {
			conn = DBUtil.getConnection();
			ps = conn.prepareStatement(queryStr);
			//debug("query str : "+queryStr);
			rs = ps.executeQuery();
			avatarVOList = new ArrayList<AvatarVO>();
			while(rs.next()){
				avatarVO = new AvatarVO();
				avatarVO.setAvatarId(rs.getString(1));
				avatarVO.setPosX(rs.getInt(2));
				avatarVO.setPosY(rs.getInt(3));
				avatarVO.setAvatarType(rs.getString(4));
				avatarVOList.add(avatarVO);
			}
			
			return avatarVOList;
		} catch (Exception e) {
			System.err.println("[ERROR] DB 오류 발생 ");
			e.printStackTrace();
		} 
		
		return null;
	}

}
